//
//  Material + Codable.swift
//  物料商行
//
//  Created by HaoDong Hong on 2017/9/18.
//  Copyright © 2017年 HaoDong Hong. All rights reserved.
//

import Foundation
import os.log

struct CodableMaterial: Codable {
    let storeId: Int = Store.storeId!
    var classId: Int
    var sessionId: String = Store.sessionID!
    var styleId: Int = 0
    var name: String = ""
    var no: String = ""
    var price: Double = 0
    var unit: String = ""
    var body: String = ""
    var color: [CodableColorOfMaterial] = []
    var component: [CodableComponentOfMaterial] = []
    var userDefine: [CodableUserDefine]? = []
    var picture: [CodablePictureOfMaterial] = []
    
    init(classId: Int) {
        self.classId = classId
    }
}
struct CodableColorOfMaterial: Codable {
    /// 编辑物料的时候用
    var type: Int? = nil
    var id: Int? = nil
    var no: String
    var color: String
    var stock: Int
    var picture: String
    init(no: String, color: String, stock: Int, picture: String) {
        self.no = no
        self.color = color
        self.stock = stock
        self.picture = picture
    }
}
struct CodableComponentOfMaterial: Codable {
    /// 编辑物料的时候用
    var type: Int? = nil
    var id: Int? = nil
    var name: String
    var percent: String
    init(name: String, percent: String) {
        self.name = name
        self.percent = percent
    }
}
struct CodableUserDefine: Codable {
    /// 编辑物料的时候用
    var type: Int? = nil
    var id: Int? = nil
    var key: String? = nil
    var value: String? = nil
    init(key: String, value: String) {
        self.key = key
        self.value = value
    }
    init(type: Int, id: Int?) {
        self.type = type
        self.id = id
    }
    init(key: String, type: Int) {
        self.key = key
        self.type = type
    }
    
}
struct CodablePictureOfMaterial: Codable {
    /// 编辑物料的时候用
    var type: Int? = nil
    var id: Int? = nil
    var img: String
    init(img: String) {
        self.img = img
    }
}


struct MaterialDetail: Codable {
    var id: Int = 0
    var classId: Int = 0
    var styleId: Int = 0
    var no: String = ""
    var name: String = ""
    var price: String = ""
    var unit: String = ""
    var bPic: String? = nil
    var suoBan: Int = 0
    var lingMai: Int = 0
    var lingMaiPrice: String? = nil
    var body: String = ""
    var attr: String = ""
    var stock: Int = 0
    var color: [CodableColorOfMaterial] = []
    var component: [CodableComponentOfMaterial] = []
    var picture: [CodablePictureOfMaterial] = []
    var userDefine: [CodableUserDefine]? = nil
}
struct EditMaterial: Codable {
    let sessionId: String = Store.sessionID!
    let storeId: Int = Store.storeId!
    var materialId: Int? = nil
    var name: String? = nil
    var no: String? = nil
    var price: Double? = nil
    var unit: String? = nil
    var body: String? = nil
    var color: [CodableColorOfMaterial] = []
    var component: [CodableComponentOfMaterial] = []
    var picture: [CodablePictureOfMaterial] = []
    var userDefine: [CodableUserDefine] = []
}

/// 入库
struct Input: Codable {
    let sessionId: String = Store.sessionID!
    let storeId: Int = Store.storeId!
    var input: [RuKu]
}
struct RuKu: Codable {
    var materialId: Int
    var colorId: Int
    var stock: Int
    var unit: String
}

/// 销售
struct ShoppingKey {
    static let materialId = "materialId"
    static let materialName = "materialName"
    static let colorNo = "colorNo"
    static let color = "color"
    static let colorId = "colorId"
    static let number = "number"
    static let price = "price"
    static let unit = "unit"
}
class ShoppingList: NSObject, NSCoding {
    var materialId: Int
    var materialName: String
    var colorNo: String
    var color: String
    var colorId: Int
    var number: String
    var price: String
    var unit: String
    static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
    static let ArchiveURL = ShoppingList.DocumentsDirectory.appendingPathComponent("ShoppingList")
    init(materialId: Int, materialName: String, colorNo: String,  color: String, colorId: Int, number: String, price: String, unit: String) {
        self.materialId = materialId
        self.materialName = materialName
        self.colorNo = colorNo
        self.color = color
        self.colorId = colorId
        self.number = number
        self.price = price
        self.unit = unit
    }
    func encode(with aCoder: NSCoder) {
        aCoder.encode(materialId, forKey: ShoppingKey.materialId)
        aCoder.encode(materialName, forKey: ShoppingKey.materialName)
        aCoder.encode(colorNo, forKey: ShoppingKey.colorNo)
        aCoder.encode(color, forKey: ShoppingKey.color)
        aCoder.encode(colorId, forKey: ShoppingKey.colorId)
        aCoder.encode(number, forKey: ShoppingKey.number)
        aCoder.encode(price, forKey: ShoppingKey.price)
        aCoder.encode(unit, forKey: ShoppingKey.unit)
    }
    required convenience init?(coder aDecoder: NSCoder) {
//        guard let materialId = aDecoder.decodeObject(forKey: ShoppingKey.materialId) as? Int else {
//            os_log("Unable to decode the name for a Meal object.", log: OSLog.default, type: .debug)
//            return nil
//        }
        let materialId = aDecoder.decodeInteger(forKey: ShoppingKey.materialId)
        let materialName = aDecoder.decodeObject(forKey: ShoppingKey.materialName) as! String
        let colorNo = aDecoder.decodeObject(forKey: ShoppingKey.colorNo) as! String
        let color = aDecoder.decodeObject(forKey: ShoppingKey.color) as! String
        let colorId = aDecoder.decodeInteger(forKey: ShoppingKey.colorId)
        let number = aDecoder.decodeObject(forKey: ShoppingKey.number) as! String
        let price = aDecoder.decodeObject(forKey: ShoppingKey.price) as! String
        let unit = aDecoder.decodeObject(forKey: ShoppingKey.unit) as! String
        
        self.init(materialId: materialId, materialName: materialName, colorNo: colorNo, color: color, colorId: colorId, number: number, price: price, unit: unit)
    }
    
}
struct Sale: Codable {
    struct XiaoShou: Codable {
        var materialId: Int
        var colorId: Int
        var number: String
        var price: String
        var unit: String
    }
    let sessionId: String = Store.sessionID!
    let storeId: Int = Store.storeId!
    var buyerPhone: String
    var way: Int
    var sale: [XiaoShou]
}

/// MARK: 记录

/// 入库记录上传的URL Params
struct RuKuRoot: Codable {
    let sessionId: String = Store.sessionID!
    let storeId: Int = Store.storeId!
    var dateType: Int?
    var beginDate: String?
    var endDate: String?
    var page: Int?
    var materialId: Int?
}

/// 入库记录返回结果
struct RuKuRecord: Codable {
    struct Record: Codable {
        var id: Int
        var storeId: Int
        var userId: String
        var created_at: String
        var updated_at: String
    }
    struct Detail: Codable {
        var materialName: String
        var materialColor: String
        var colorNo: String
        var colorPicture: String
        var stock: String
        var unit: String
    }
    var record: Record
    var detail: [Detail]
}


/// 交易记录上传的URL Params
struct SaleRoot: Codable {
    let sessionId: String = Store.sessionID!
    let storeId: Int = Store.storeId!
    var dateType: Int?
    var beginDate: String?
    var endDate: String?
    var page: Int?
    var materialId: Int?
}

/// 交易记录返回结果
struct SaleRecord: Codable {
    struct Record: Codable {
        var id: Int
        var storeId: Int
        var buyerPhone: String
        var created_at: String
        var way: Int
        var updated_at: String
    }
    struct Detail: Codable {
        var materialName: String
        var materialColor: String
        var colorNo: String
        var colorPicture: String
        var price: String
        var number: String
        var unit: String
    }
    var record: Record
    var detail: [Detail]
}

struct SuoBanRoot: Codable {
    let sessionId: String = Store.sessionID!
    let storeId: Int = Store.storeId!
    var dateType: Int?
    var beginDate: String?
    var endDate: String?
    var page: Int?
    var materialId: Int?
}

/// 交易记录返回结果
struct SuoBanRecord: Codable {
    var buyerPhone: String
    var materialName: String
    var colorPicture: String
    var created_at: String
}

/// 订阅记录返回结果
struct DingYueRecord: Codable {
    var buyerPhone: String
    var date: String
}

/// 共享记录上传值
struct ShareRoot: Codable {
    let sessionId: String = Store.sessionID!
    let storeId: Int = Store.storeId!
    var material: Int
}

/// 共享记录返回结果
struct ShareRecord: Codable {
    var scanstoreName: String    //扫描的商号名称
    var stock: Int       //扫描的库存量
    var materialName: String    //面料名称
    var materialNo: String    //面料货号
    var unit: String    //单位
    var time: String      //商品共享的时间
}

/// 授信记录Protocol
protocol ShouXinRecord { }
/// 买家授信记录返回结果
struct BuyerShouXinRecord: Codable, ShouXinRecord {
    var buyerPhone: String
    var creditId: Int
    var limit: String
    var date: String
    var status: Int
    var buyerId: Int
    var createAt: String
}
/// 商家授信记录放回结果
struct SalerShouXinRecord: Codable, ShouXinRecord {
    var userId: String
    var creditId: Int
    var limit: String
    var date: String
    var status: Int
    var storeId: Int
    var createAt: String
}
/// 创建授信
struct ShouXinRoot: Codable {
    let sessionId: String = Store.sessionID!
    let storeId: Int = Store.storeId!
    var lendStoreId: Int
    var limit: Double
    var date: String
    var status: Int
}
/// 欠款记录
struct IndebtRecord: Codable {
    var logo: String
    var storeName: String
    var userId: String
    var creditId: Int
    var limit: String
    var date: String
    var status: Int
    var lendStoreId: Int
    var createAt: String
}




